package cn.piesat.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: demo
 * @date: 2020年6月19日
 * @Document : 基于jackson的工具类
 */
public class JsonUtil {
	private static ObjectMapper mapper = new ObjectMapper();
	private static final String SIMPLE_FORMAT_DATE = "yyyy-MM-dd HH:mm:ss";
	private static final String  PARSE_SYMBOL="[";
	private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);
	static {
		// 对象的所有字段全部列入，还是其他的选项，可以忽略null等
		mapper.setSerializationInclusion(Include.ALWAYS);
		// 设置Date类型的序列化及反序列化格式
		mapper.setDateFormat(new SimpleDateFormat(SIMPLE_FORMAT_DATE));
		// 忽略空Bean转json的错误
		mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		// 忽略未知属性，防止json字符串中存在，java对象中不存在对应属性的情况出现错误
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		// 注册一个时间序列化及反序列化的处理模块，用于解决jdk8中localDateTime等的序列化问题
		mapper.registerModule(new JavaTimeModule());
		// 允许转换[] 为list
		mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
	}

	/**
	 * 转换为List
	 */
	public static <T> List<T> toList(String json, Class<T> clazz) {
		try {
			return mapper.readValue(json, new TypeReference<List<T>>() {
			});
		} catch (Exception e) {
			logger.error("JSON转换异常==>", e);
		}
		return null;
	}

	/**
	 * 转换为List<Map>
	 */
//	public static List<Map<String, Object>> toListMap(String json) {
//		try {
//			return mapper.readValue(json, new TypeReference<List<HashMap<String, Object>>>() {
//			});
//		} catch (Exception e) {
//			logger.error("JSON转换异常==>", e);
//		}
//		return null;
//	}

	public static <T> List<T> toListBean(String json, Class<T> clazz) {
		try {
			return mapper.readValue(json, new TypeReference<List<T>>() {
			});

		} catch (Exception e) {
			logger.error("JSON转换异常==>", e);
		}
		return null;
	}

	/**
	 * json转换为Map
	 */
	@SuppressWarnings("unchecked")
	public static <T> Map<String, Object> toMap(String jsonString) {
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		try {
			return mapper.readValue(jsonString, HashMap.class);
		} catch (Exception e) {
			logger.error("JSON转换异常==>", e);
		}
		return null;
	}

	/**
	 * 转换为bean
	 */
	public static <T> T toBean(String json, Class<T> clazz) {
		try {
			return mapper.readValue(json, clazz);
		} catch (Exception e) {
			logger.error("JSON转换异常==>", e);
		}
		return null;
	}

	/**
	 * 自定义日期转化
	 */
	public static String toJsonPattenDate(Object obj, String pattern) {
		SimpleDateFormat fmt = new SimpleDateFormat(pattern);
		mapper.setDateFormat(fmt);
		try {
			return mapper.writeValueAsString(obj);
		} catch (Exception e) {
			logger.error("JSON转换异常==>", e);
		}
		return null;
	}

	/**
	 * 转换忽略null
	 */
	public static String toJsonIgnoreNull(Object obj) {
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		try {
			return mapper.writeValueAsString(obj);
		} catch (Exception e) {
			logger.error("JSON转换异常==>", e);
		}
		return null;
	}

	/**
	 * object转json
	 */
	public static String toJson(Object obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (Exception e) {
			logger.error("JSON转换异常==>", e);
		}
		return null;
	}

	/**
	 * object 转json
	 * 
	 * @param obj
	 * @return
	 */
	public static String toString(Object obj) {
		return toJson(obj);
	}

	/**
	 * 格式化json
	 * 
	 * @param obj
	 * @throws Exception
	 */
//	public static String getPrettifyJson(Object obj) {
//		mapper.enable(SerializationFeature.INDENT_OUTPUT);
//		String result = "";
//		try {
//			String str = mapper.writeValueAsString(obj);
//			if (str.indexOf(PARSE_SYMBOL) < 1) {
//				result = toJson(toListMap(str));
//			} else {
//				result = toJson(toMap(str));
//			}
//		} catch (Exception e) {
//			logger.error("JSON转换异常==>", e);
//			result = "JSON转换异常";
//		}
//		return "\n".concat(result);
//	}

}
